package com.fengba.nanzhi.tabbar01;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
 * Created by 89311 on 2018/4/11.
 */

public class PSQAdapter extends BaseAdapter {
    private Context context;
    private JSONArray jsonArray;
    private Tabbar01 notice;
    private JSONObject job;
    String title;
    String content;
    String time;
    private LayoutInflater mLayoutInflater;
    private Boolean flage = true;


    public PSQAdapter(Context context, Tabbar01 add) {
        this.context = context;
        this.notice = add;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        jsonArray = notice.getJsonArray();
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
        {
            return 10;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    static class  ViewHolder
    {
        public ImageView imageView;
        public TextView textView;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder =null;
        if (view==null)
        {
            view=mLayoutInflater.inflate(R.layout.fx_psq_view,null);
            holder = new ViewHolder();
            view.setTag(holder);
            holder.imageView = view.findViewById(R.id.Piv01);
            holder.textView = view.findViewById(R.id.Ptv01);
        }else
            {
                holder = (ViewHolder)view.getTag();
            }
        //赋值
            try {
                JSONObject a = (JSONObject) jsonArray.get(i);
                Glide.with(context).load(a.get("icon").toString()).into(holder.imageView);
                holder.textView.setText(a.get("name").toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        return view;
    }

    /*@Override
    public LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.fx_psq_view,parent,false));
    }
    @Override
    public void onBindViewHolder(LinerViewHolder holder, int position) {
        title="";
        content="";
        try {
            job = (JSONObject) jsonArray.get(position);
             title= job.get("title").toString();
            content= job.get("content").toString();
            time = job.get("createtime").toString();
            String data= ConverToDate(time);
            holder.Ntv01.setText(title);
            holder.Ntv02.setText(content);
            holder.Ntoday01.setText(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        jsonArray = fx_notice.getJsonArray();
        ////System.out.println("aaaaaa"+jsonArray.toString());
        if(jsonArray!=null) {
            return jsonArray.length();
        }else
            {
                return 0;
            }
    }

    /*class LinerViewHolder extends RecyclerView.ViewHolder{
        private TextView Ntv01;
        private TextView Ntv02;
        private TextView Ntoday01;
        public LinerViewHolder(View itemView) {
            super(itemView);
            Ntv01 = itemView.findViewById(R.id.Ntv01);
            Ntv02 = itemView.findViewById(R.id.Ntv02);
            Ntoday01 = itemView.findViewById(R.id.Ntoday01);
        }
    }

    public final String ConverToDate(String strDate) throws Exception
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d= df.parse(strDate);
        String time = df.format(d);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new Date());
        if (time.equals(date))
        {
            DateFormat df1 = new SimpleDateFormat("hh:mm");
            Date d1= df.parse(strDate);
            String a= df1.format(d1);
            return " 今天:"+a+" ";
        }else
            {
                return " "+time+" ";
            }

    }*/
}
